package com.yf.dxapp.module.app.controller.admin.favorite;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import com.yf.dxapp.module.app.controller.admin.favorite.vo.FavoritePageReqVO;
import com.yf.dxapp.module.app.controller.admin.favorite.vo.FavoriteRespVO;
import com.yf.dxapp.module.app.controller.admin.favorite.vo.FavoriteSaveReqVO;
import com.yf.dxapp.module.app.dal.dataobject.favorite.FavoriteDO;
import com.yf.dxapp.module.app.service.favorite.FavoriteService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 收藏")
@RestController
@RequestMapping("/dxapp/favorite")
@Validated
public class FavoriteController {

    @Resource
    private FavoriteService favoriteService;

    @PostMapping("/create")
    @Operation(summary = "创建收藏")
    @PreAuthorize("@ss.hasPermission('app:favorite:create')")
    public CommonResult<Long> createFavorite(@Valid @RequestBody FavoriteSaveReqVO createReqVO) {
        return success(favoriteService.createFavorite(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新收藏")
    @PreAuthorize("@ss.hasPermission('app:favorite:update')")
    public CommonResult<Boolean> updateFavorite(@Valid @RequestBody FavoriteSaveReqVO updateReqVO) {
        favoriteService.updateFavorite(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除收藏")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('app:favorite:delete')")
    public CommonResult<Boolean> deleteFavorite(@RequestParam("id") Long id) {
        favoriteService.deleteFavorite(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得收藏")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('app:favorite:query')")
    public CommonResult<FavoriteRespVO> getFavorite(@RequestParam("id") Long id) {
        FavoriteDO favorite = favoriteService.getFavorite(id);
        return success(BeanUtils.toBean(favorite, FavoriteRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得收藏分页")
    @PreAuthorize("@ss.hasPermission('app:favorite:query')")
    public CommonResult<PageResult<FavoriteRespVO>> getFavoritePage(@Valid FavoritePageReqVO pageReqVO) {
        PageResult<FavoriteDO> pageResult = favoriteService.getFavoritePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, FavoriteRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出收藏 Excel")
    @PreAuthorize("@ss.hasPermission('app:favorite:export')")
    
    public void exportFavoriteExcel(@Valid FavoritePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<FavoriteDO> list = favoriteService.getFavoritePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "收藏.xls", "数据", FavoriteRespVO.class,
                        BeanUtils.toBean(list, FavoriteRespVO.class));
    }

}